<?php
declare (strict_types = 1);

namespace app\model\table\season;


use app\model\table\union\Union;
use app\model\table\user\User;

/**
 * @mixin \think\Model
 */
class Rank
{
    // 赛季排行榜
    public function getRanSaiji($userObj,$limit)
    {
        $where['limit'] = $limit;
        $where['orderBy'] = "score_season desc";
        $objs = User::instance()->findMore($where);
        $JoinWith=['UnionInfo'];
        User::instance()->dealJoinWith($objs,$JoinWith);
        $rs = array();
        foreach ($objs as $key=>$val){
            $rs[$val['id']] = array(
                'user_id' => $val['id'],
                'user_name' => $val['name'],
                'score' => $val['score_season'],
                'union_id' => $val['UnionInfo']['id'] ?? "",
                'union_name' => $val['UnionInfo']['name'] ?? "",
                'union_icon' => $val['UnionInfo']['icon'] ?? "",
                'rank' => $key + 1,
            );
        }

        if(isset($rs[$userObj['id']])){
            // 代表我在排行榜里面
            $my = $rs[$userObj['id']];
        }else{
            $userObjs = [$userObj];
            $JoinWith=['UnionInfo'];
            User::instance()->dealJoinWith($userObjs,$JoinWith);
            $my = array(
                'user_id' => $userObjs[0]['id'],
                'user_name' => $userObjs[0]['name'],
                'score' => $userObjs[0]['score_season'],
                'union_id' => $userObjs[0]['UnionInfo']['id'] ?? "",
                'union_name' => $userObjs[0]['UnionInfo']['name'] ?? "",
                'union_icon' => $userObjs[0]['UnionInfo']['icon'] ?? "",
                'rank' => $limit."+",
            );
        }

        return array(
            'list' => array_values($rs),
            'my' => $my
        );
    }

    /**
     * 本地排行榜
     * @return mixed
     */
    public function getRanLocal($userObj,$limit)
    {
        $where['limit'] = $limit;
        $where['orderBy'] = "score desc";
        $where[] = ['nation','=',$userObj['nation']];
        $objs = User::instance()->findMore($where);
        $JoinWith=['UnionInfo'];
        User::instance()->dealJoinWith($objs,$JoinWith);
        $rs = array();
        foreach ($objs as $key=>$val){
            $rs[$val['id']] = array(
                'user_id' => $val['id'],
                'user_name' => $val['name'],
                'score' => $val['score'],
                'union_id' => $val['UnionInfo']['id'] ?? "",
                'union_name' => $val['UnionInfo']['name'] ?? "",
                'union_icon' => $val['UnionInfo']['icon'] ?? "",
                'rank' => $key + 1,
            );
        }

        if(isset($rs[$userObj['id']])){
            // 代表我在排行榜里面
            $my = $rs[$userObj['id']];
        }else{
            $userObjs = [$userObj];
            $JoinWith=['UnionInfo'];
            User::instance()->dealJoinWith($userObjs,$JoinWith);
            $my = array(
                'user_id' => $userObjs[0]['id'],
                'user_name' => $userObjs[0]['name'],
                'score' => $userObjs[0]['score'],
                'union_id' => $userObjs[0]['UnionInfo']['id'] ?? "",
                'union_name' => $userObjs[0]['UnionInfo']['name'] ?? "",
                'union_icon' => $userObjs[0]['UnionInfo']['icon'] ?? "",
                'rank' => $limit."+",
            );
        }

        return array(
            'list' => array_values($rs),
            'my' => $my
        );
    }

    /**
     * 全部排行榜
     * @return mixed
     */
    public function getRanAll($userObj,$limit)
    {
        $where['limit'] = $limit;
        $where['orderBy'] = "score desc";
        $objs = User::instance()->findMore($where);
        $JoinWith=['UnionInfo'];
        User::instance()->dealJoinWith($objs,$JoinWith);
        $rs = array();
        foreach ($objs as $key=>$val){
            $rs[$val['id']] = array(
                'user_id' => $val['id'],
                'user_name' => $val['name'],
                'score' => $val['score'],
                'union_id' => $val['UnionInfo']['id'] ?? "",
                'union_name' => $val['UnionInfo']['name'] ?? "",
                'union_icon' => $val['UnionInfo']['icon'] ?? "",
                'rank' => $key + 1,
            );
        }

        if(isset($rs[$userObj['id']])){
            // 代表我在排行榜里面
            $my = $rs[$userObj['id']];
        }else{
            $userObjs = [$userObj];
            $JoinWith=['UnionInfo'];
            User::instance()->dealJoinWith($userObjs,$JoinWith);
            $my = array(
                'user_id' => $userObjs[0]['id'],
                'user_name' => $userObjs[0]['name'],
                'score' => $userObjs[0]['score'],
                'union_id' => $userObjs[0]['UnionInfo']['id'] ?? "",
                'union_name' => $userObjs[0]['UnionInfo']['name'] ?? "",
                'union_icon' => $userObjs[0]['UnionInfo']['icon'] ?? "",
                'rank' => $limit."+",
            );
        }

        return array(
            'list' => array_values($rs),
            'my' => $my
        );
    }

    /**
     * 工会排行榜
     * @return mixed
     */
    public function getRanUnion($limit)
    {
        $where['limit'] = $limit;
        $where['orderBy'] = "score desc";
        $objs =  Union::instance()->findMore($where);
        foreach ($objs as $val){
            $rs[] = array(
                'union_id' => $val['id'],
                'union_name' => $val['name'],
                'union_icon' => $val['icon'],
                'score' => $val['score'],
            );
        }
        return array(
            'list' => $rs,
        );
    }

    // 名人堂 获取每个赛季的前三名
    public function getMingrentang()
    {
        // 获取历史赛季排行榜
        $where['orderBy'] = "season_id desc,ranking asc";
        $objs = SeasonScoreLog::instance()->findMore($where);
        $JoinWith=['UserInfo','SeasonConfigInfo','UnionInfo'];
        SeasonScoreLog::instance()->dealJoinWith($objs,$JoinWith);
        $rs = array();
        foreach ($objs as $val){
            $rs[$val['season_id']] = isset($rs[$val['season_id']]) ? $rs[$val['season_id']] : array(
                'season_id' => $val['SeasonConfigInfo']['id'],
                'season_name' => $val['SeasonConfigInfo']['name'],
                'list' => array()
            );
            $rs[$val['season_id']]['list'][] = array(
                'user_id' => $val['UserInfo']['id'],
                'user_name' => $val['UserInfo']['name'],
                'ranking' => $val['ranking'],
                'score' => $val['score'],
                'union_id' => $val['UnionInfo']['id'] ?? "",
                'union_name' => $val['UnionInfo']['name'] ?? "",
                'union_icon' => $val['UnionInfo']['icon'] ?? "",
            );
        }
        return array_values($rs);
    }
}
